<?php
namespace models\service;
//use entities;

class FinanceService {

	protected $ci;
	public $invLastId;
	public $invItemLastId;
	public $invTaxLastId;
	public $invTaxItemLastId;
	public $delivNoteLastId;
	public $delivItemLastId;
	public $quotationLastId;
	public $quotationItemLastId;
	public $inquiryLastId;
	public $inquiryItemLastId;
	public $poLastId;
	public $poItemLastId;
	public $taxLastId;
	public $taxItemLastId;
	public function __construct() {
		$this -> ci = &get_instance();
	}

	public function findAllStock($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\Item') -> findAllAsObject();
	}

	public function findAllUser($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\User') -> findAllAsObject();
	}

	public function findAllProfiles($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\Profile') -> findAllAsObject();
	}

	public function findAllInvoice($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\Invoice') -> findAllAsObject();
	}

	public function findAllInvoiceTax($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\InvoiceTax') -> findAllAsObject();
	}

	public function findAllFakturPajak($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\Tax') -> findAllAsObject($indexAndCount);
	}

	public function findAllDeliveryNote($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\DeliveryNote') -> findAllAsObject($indexAndCount);
	}

	public function insertInvoice($param) {
		if ($param != null) {
			$inv = new \models\entities\Invoice;
			$inv -> setToCompanyName($param['companyName']);
			$inv -> setToCompanyAddress($param['companyAddress']);
			$inv -> setToCompanyAttn($param['attn']);
			$inv -> setToCompanyNo($param['no']);
			$invoiceDate = new \DateTime($param['invoiceDate'], new \DateTimeZone("Asia/Jakarta"));
			$inv -> setInvoiceDate($invoiceDate);
			$inv -> setReff($param['reff']);
			$inv -> setCreatedDate(new \DateTime("now"));
			$inv -> setFootBankName($param['bankName']);
			$inv -> setFootBankAcc($param['bankAcc']);
			$inv -> setFootBankName2($param['bankName2']);
			$inv -> setFootBankAcc2($param['bankAcc2']);
			$inv -> setIsPo($param['isPo']);
			$inv -> setIsServiceReport($param['isServiceReport']);
			$inv -> setIsDeliverNote($param['isDeliverNote']);
			$inv -> setCompanySignature($param['companySignature']);
			$inv -> setNameSignature($param['nameSignature']);
			$inv -> setPosition($param['position']);
			$inv -> setTerms($param['terms']);
			$this -> ci -> em -> persist($inv);
			$this -> ci -> em -> flush();
			$this -> invLastId = $inv -> getId();
			return TRUE;
		} else {
			return FALSE;
		}

	}

	public function duplicateInvoice($inv) {
		if ($inv -> getId() > 0) {
			$newInv = new \models\entities\Invoice;
			$newInv -> setToCompanyName($inv -> getToCompanyName());
			$newInv -> setToCompanyAddress($inv -> getToCompanyAddress());
			$newInv -> setToCompanyAttn($inv -> getToCompanyAttn());

			// get counter
			$criteria = array('invoice' => $inv);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterInvoice') -> findOneBy($criteria);
			//var_dump($counter);
			if ($counter != NULL) {
				$invoiceNo = $inv -> getToCompanyNo() . "(" . ($counter -> getCounter() + 1) . ")";
				$counter -> setCounter($counter -> getCounter() + 1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			} else {
				$invoiceNo = $inv -> getToCompanyNo() . "(1)";
				$counter = new \models\entities\CounterInvoice;
				$counter -> setInvoice($inv);
				$counter -> setCounter(1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			}

			$newInv -> setToCompanyNo($invoiceNo);

			if ($inv -> getInvoiceDate() != "") {
				//$newInv -> setInvoiceDate($inv -> getInvoiceDate());
				$newInv -> setInvoiceDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
			}
			$newInv -> setCompany($inv -> getCompany());
			$newInv -> setReff($inv -> getReff());
			$newInv -> setTerms($inv -> getTerms());
			$newInv -> setNote($inv -> getNote());
			$newInv -> setFootBankName($inv -> getFootBankName());
			$newInv -> setFootBankAcc($inv -> getFootBankAcc());
			$newInv -> setFootBankName2($inv -> getFootBankName2());
			$newInv -> setFootBankAcc2($inv -> getFootBankAcc2());
			$newInv -> setBankRek($inv -> getBankRek());
			$newInv -> setBankRek2($inv -> getBankRek2());
			$newInv -> setSwiftCode($inv -> getSwiftCode());
			$newInv -> setIsPo($inv -> getIsPo());
			$newInv -> setIsServiceReport($inv -> getIsServiceReport());
			$newInv -> setIsDeliverNote($inv -> getIsDeliverNote());
			$newInv -> setCompanySignature($inv -> getCompanySignature());
			$newInv -> setNameSignature($inv -> getNameSignature());
			$newInv -> setPosition($inv -> getPosition());
			$newInv -> setCreatedDate(new \DateTime("now"));
			$newInv -> setDisc($inv -> getDisc());
			$newInv -> setDpPersen($inv -> getDpPersen());
			$newInv -> setDpValue($inv -> getDpValue());
			$newInv -> setTotal($inv -> getTotal());
			$newInv -> setAttach($inv->getAttach());
			$newInv -> setAttach2($inv->getAttach2());
			$newInv -> setAttach3($inv->getAttach3());
			$newInv -> setCurrency($inv->getCurrency());
			// $newInv -> setVarFieldValue($inv -> getVarFieldValue());

			$this -> ci -> em -> persist($newInv);
			$this -> ci -> em -> flush();

			return $newInv;
		}
		return FALSE;
	}

	public function duplicateInvoiceTax($inv) {
		if ($inv -> getId() > 0) {
			$newInv = new \models\entities\InvoiceTax;
			$newInv -> setToCompanyName($inv -> getToCompanyName());
			$newInv -> setToCompanyAddress($inv -> getToCompanyAddress());
			$newInv -> setToCompanyAttn($inv -> getToCompanyAttn());

			// get counter
			$criteria = array('invoiceTax' => $inv);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterInvoiceTax') -> findOneBy($criteria);

			if ($counter != NULL) {
				$invoiceNo = $inv -> getToCompanyNo() . "(" . ($counter -> getCounter() + 1) . ")";
				$counter -> setCounter($counter -> getCounter() + 1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			} else {
				$invoiceNo = $inv -> getToCompanyNo() . "(1)";
				$counter = new \models\entities\CounterInvoiceTax;
				$counter -> setInvoiceTax($inv);
				$counter -> setCounter(1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			}

			$newInv -> setToCompanyNo($invoiceNo);
			$newInv -> setCompany($inv->getCompany());
			if ($inv -> getInvoiceDate() != "") {
				// $newInv -> setInvoiceDate($inv -> getInvoiceDate());
				$newInv -> setInvoiceDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
			}
			$newInv -> setReff($inv -> getReff());
			$newInv -> setTerms($inv -> getTerms());
			$newInv -> setNote($inv -> getNote());
			$newInv -> setFootBankName($inv -> getFootBankName());
			$newInv -> setFootBankAcc($inv -> getFootBankAcc());
			$newInv -> setFootBankName2($inv -> getFootBankName2());
			$newInv -> setFootBankAcc2($inv -> getFootBankAcc2());
			$newInv -> setBankRek($inv -> getBankRek());
			$newInv -> setBankRek2($inv -> getBankRek2());
			$newInv -> setSwiftCode($inv -> getSwiftCode());
			$newInv -> setIsPo($inv -> getIsPo());
			$newInv -> setIsServiceReport($inv -> getIsServiceReport());
			$newInv -> setIsDeliverNote($inv -> getIsDeliverNote());
			$newInv -> setCompanySignature($inv -> getCompanySignature());
			$newInv -> setNameSignature($inv -> getNameSignature());
			$newInv -> setPosition($inv -> getPosition());
			$newInv -> setCreatedDate(new \DateTime("now"));
			$newInv -> setDpPersen($inv -> getDpPersen());
			$newInv -> setDpValue($inv -> getDpValue());
			$newInv -> setSubTotal($inv -> getSubTotal());
			$newInv -> setPPn($inv -> getPpn());
			$newInv -> setTotal($inv -> getTotal());
			$newInv -> setVarField($inv -> getVarField());
			$newInv -> setVarFieldPersen($inv -> getVarFieldPersen());
			$newInv -> setVarFieldValue($inv -> getVarFieldValue());
			$newInv -> setDisc($inv -> getDisc());
			$newInv -> setAttach($inv->getAttach());
			$newInv -> setAttach2($inv->getAttach2());
			$newInv -> setAttach3($inv->getAttach3());
			$newInv -> setCurrency($inv->getCurrency());

			$this -> ci -> em -> persist($newInv);
			$this -> ci -> em -> flush();

			return $newInv;
		}
		return FALSE;
	}

	public function saveInvoice($param) {
		// echo "<pre>";print_r($param);die();
		if ($param != null) {
			$mode = "save";
			if ($param['id'] > 0) {
				$criteria = array('id' => $param['id']);
				$inv = $this -> ci -> em -> getRepository('\models\entities\Invoice') -> findOneBy($criteria);
			} else {
				$inv = new \models\entities\Invoice;
				$mode = "add";
			}

			$inv -> setToCompanyName($param['companyName']);
			$inv -> setToCompanyAddress($param['companyAddress']);
			$inv -> setToCompanyAttn($param['attn']);
			$inv -> setToCompanyNo($param['no']);

			if ($param['invoiceDate'] != "") {
				$invoiceDate = new \DateTime($param['invoiceDate'], new \DateTimeZone("Asia/Jakarta"));
				$inv -> setInvoiceDate($invoiceDate);
			} else {
				$inv -> setInvoiceDate(NULL);
			}

			if ($mode == "add") {
				$inv -> setCreatedDate(new \DateTime("now"));
			}
			if ($param['company'] > 0){
				$criteria = array('id' => $param['company']);
				$company = $this->ci->em->getRepository('\models\entities\Company')->findOneBy($criteria);
				$inv -> setCompany($company);	
			} 			
			
			$inv -> setReff($param['reff']);
			$inv -> setTerms($param['terms']);
			$inv -> setNote($param['note']);
			$inv -> setFootBankName($param['bankName']);
			$inv -> setFootBankAcc($param['bankAcc']);
			$inv -> setFootBankName2($param['bankName2']);
			$inv -> setFootBankAcc2($param['bankAcc2']);
			$inv -> setBankRek($param['bankRek']);
			$inv -> setBankRek2($param['bankRek2']);
			$inv -> setSwiftCode($param['swiftCode']);
			$inv -> setIsPo($param['isPo']);
			$inv -> setIsServiceReport($param['isServiceReport']);
			$inv -> setIsDeliverNote($param['isDeliverNote']);
			$inv -> setCompanySignature($param['companySignature']);
			$inv -> setNameSignature($param['nameSignature']);
			$inv -> setPosition($param['position']);
			$inv -> setDisc($param['disc']);
			$inv -> setDpPersen($param['dpPersen']);
			$inv -> setDpValue($param['dpValue']);
			$inv -> setTotal($param['total']);
			$inv -> setCurrency($param['currency']);
			// $inv -> setSubTotal($param['subTotal']);
			// $inv->setVarField($param['varField']);
			// $inv->setVarFieldPersen($param['varFieldPersen']);
			// $inv->setVarFieldValue($param['varFieldValue']);
			//echo '<pre>'; print_r($_FILES); die();
			if (array_key_exists('attach',$_FILES)){
				if ($_FILES['attach'] && $_FILES['attach']['error'] == 0) {
					$config['upload_path'] = './attach/';
					$config['allowed_types'] = 'pdf';
					//$config['max_size']	= '100';
					//$config['max_width']  = '1024';
					//$config['max_height']  = '768';
	
					$this -> ci -> load -> library('upload', $config);
	
					if (!$this -> ci -> upload -> do_upload('attach')) {
						$error = array('error' => $this -> ci -> upload -> display_errors());
	
						//$this->load->view('upload_form', $error);
						print_r($error);
						return FALSE;
					} else {
						$data = array('upload_data' => $this -> ci -> upload -> data());
	
						//$this->load->view('upload_success', $data);
						//print_r($data);
						$inv -> setAttach('attach/'.$data['upload_data']['file_name']);
					}
				}
			}
			
			if (array_key_exists('attach2',$_FILES)){
				if ($_FILES['attach2'] && $_FILES['attach2']['error'] == 0) {
					$config['upload_path'] = './attach/';
					$config['allowed_types'] = 'pdf';
					//$config['max_size']	= '100';
					//$config['max_width']  = '1024';
					//$config['max_height']  = '768';
	
					$this -> ci -> load -> library('upload', $config);
	
					if (!$this -> ci -> upload -> do_upload('attach2')) {
						$error = array('error' => $this -> ci -> upload -> display_errors());
	
						//$this->load->view('upload_form', $error);
						print_r($error);
						return FALSE;
					} else {
						$data = array('upload_data' => $this -> ci -> upload -> data());
	
						//$this->load->view('upload_success', $data);
						//print_r($data);
						$inv -> setAttach2('attach/'.$data['upload_data']['file_name']);
					}
				}
			}
			
			if (array_key_exists('attach3',$_FILES)){
				if ($_FILES['attach3'] && $_FILES['attach3']['error'] == 0) {
					$config['upload_path'] = './attach/';
					$config['allowed_types'] = 'pdf';
					//$config['max_size']	= '100';
					//$config['max_width']  = '1024';
					//$config['max_height']  = '768';
	
					$this -> ci -> load -> library('upload', $config);
	
					if (!$this -> ci -> upload -> do_upload('attach3')) {
						$error = array('error' => $this -> ci -> upload -> display_errors());
	
						//$this->load->view('upload_form', $error);
						print_r($error);
						return FALSE;
					} else {
						$data = array('upload_data' => $this -> ci -> upload -> data());
	
						//$this->load->view('upload_success', $data);
						//print_r($data);
						$inv -> setAttach3('attach/'.$data['upload_data']['file_name']);
					}
				}
			}			
			

			$this -> ci -> em -> persist($inv);
			$this -> ci -> em -> flush();
			$this -> invLastId = $inv -> getId();
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function saveTax($param) {
		// echo "<pre>";print_r($param);die();
		if ($param != null) {
			if ($param['id'] > 0) {
				$criteria = array('id' => $param['id']);
				$tax = $this -> ci -> em -> getRepository('\models\entities\Tax') -> findOneBy($criteria);
			} else {
				$tax = new \models\entities\Tax;
			}

			$tax -> setKode($param['kode']);
			$tax -> setNamaPengusaha($param['namaPengusaha']);
			$tax -> setAlamatPengusaha($param['alamatPengusaha']);
			$tax -> setNpwpPengusaha($param['npwpPengusaha']);
			$tax -> setCreatedDate(new \DateTime("now"));
			$taxDate = new \DateTime($param['taxDate'], new \DateTimeZone("Asia/Jakarta"));
			$tax -> setTanggal($taxDate);
			$tax -> setNamaPembeli($param['namaPembeli']);
			$tax -> setAlamatPembeli($param['alamatPembeli']);
			$tax -> setNpwpPembeli($param['npwpPembeli']);
			$tax -> setNote($param['note']);			
			$tax -> setPajakBarangMewahTarif($param['tarif']);
			$tax -> setPajakBarangMewahDpp($param['dpp']);
			$tax -> setPajakBarangMewahPpnbm($param['ppnbm']);
			$tax -> setPajakBarangMewahTarif2($param['tarif2']);
			$tax -> setPajakBarangMewahDpp2($param['dpp2']);
			$tax -> setPajakBarangMewahPpnbm2($param['ppnbm2']);
			$tax -> setPajakBarangMewahTarif3($param['tarif3']);
			$tax -> setPajakBarangMewahDpp3($param['dpp3']);
			$tax -> setPajakBarangMewahPpnbm3($param['ppnbm3']);
			$tax -> setPajakBarangMewahTarif4($param['tarif4']);
			$tax -> setPajakBarangMewahDpp4($param['dpp4']);
			$tax -> setPajakBarangMewahPpnbm4($param['ppnbm4']);
			$tax -> setJumlah($param['jumlah']);
			/*
			 if ($tax->getKurs() != $param['kurs']){
			 if ($param['kurs2'] == 0){
			 $kurs = 1;
			 } else {
			 $kurs = $tax->getKurs();
			 $kurs = $kurs * 1 / $param['kurs2'];
			 }
			 $taxId = $tax->getId();
			 $criteria = array('invoiceTax' => $tax->getInvoiceTax());
			 $invoiceTaxItems = $this->ci->em->getRepository('\models\entities\InvoiceTaxItem')->findBy($criteria);
			 if (count($invoiceTaxItems) > 0){
			 $i = 0;
			 $totalAmount = 0;
			 foreach ($invoiceTaxItems as $item){
			 $itemPrice[$i] = $item->getPrice() * $kurs;
			 $amount[$i] = $itemPrice[$i] * $item->getQty();
			 $totalAmount += $amount[$i];
			 $item->setPrice($itemPrice[$i]);
			 $this -> ci -> em -> persist($item);
			 $this -> ci -> em -> flush();
			 }

			 $dp = $tax->getInvoiceTax()->getDpPersen() / 100 * $totalAmount;
			 $disc = $tax->getInvoiceTax()->getDisc();
			 $subTotal = $totalAmount - $disc - $dp;
			 $ppn = 0.1 * $subTotal;
			 $total = $subTotal + $ppn - $tax->getInvoiceTax()->getVarFieldValue();
			 $invTax = $tax->getInvoiceTax();
			 $invTax->setDpValue($dp);
			 $invTax->setSubTotal($subTotal);
			 $invTax->setPpn($ppn);
			 $invTax->setTotal($total);
			 $this -> ci -> em -> persist($invTax);
			 $this -> ci -> em -> flush();
			 }
			 } */
			$tax -> setKurs($param['kurs']);
			$tax -> setKurs2($param['kurs2']);
			$tax -> setNoteKurs($param['noteKurs']);
			
			$tax -> setTempat($param['tempatTTD']);
			if ($param['tanggalTTD'] != "") {
				$tanggalTTD = new \DateTime($param['tanggalTTD'], new \DateTimeZone("Asia/Jakarta"));
				$tax -> setTanggalTTD($tanggalTTD);
			}
			$tax -> setNama($param['namaTTD']);
			$tax -> setPosisi($param['jabatanTTD']);

			$this -> ci -> em -> persist($tax);
			$this -> ci -> em -> flush();
			$this -> taxLastId = $tax -> getId();
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function saveInvoiceTax($param) {
		// echo "<pre>"; print_r($param);die();
		if ($param != null) {
			$createTax = FALSE;
			if ($param['id'] > 0) {
				$criteria = array('id' => $param['id']);
				$inv = $this -> ci -> em -> getRepository('\models\entities\InvoiceTax') -> findOneBy($criteria);
			} else {
				$inv = new \models\entities\InvoiceTax;
				$createTax = TRUE;
			}

			$inv -> setToCompanyName($param['companyName']);
			$inv -> setToCompanyAddress($param['companyAddress']);
			$inv -> setToCompanyAttn($param['attn']);
			$inv -> setToCompanyNo($param['no']);
			if ($param['invoiceDate'] != "") {
				$invoiceDate = new \DateTime($param['invoiceDate'], new \DateTimeZone("Asia/Jakarta"));
				$inv -> setInvoiceDate($invoiceDate);
			} else {
				$inv -> setInvoiceDate(NULL);
			}

			if ($createTax == TRUE) {
				$inv -> setCreatedDate(new \DateTime("now"));
			}
			
			if ($param['company'] > 0){
				$criteria = array('id' => $param['company']);
				$company = $this->ci->em->getRepository('\models\entities\Company')->findOneBy($criteria);
				$inv -> setCompany($company);	
			} 	
			$inv -> setReff($param['reff']);
			$inv -> setTerms($param['terms']);
			$inv -> setNote($param['note']);
			$inv -> setFootBankName($param['bankName']);
			$inv -> setFootBankAcc($param['bankAcc']);
			$inv -> setFootBankName2($param['bankName2']);
			$inv -> setFootBankAcc2($param['bankAcc2']);
			$inv -> setBankRek($param['bankRek']);
			$inv -> setBankRek2($param['bankRek2']);
			$inv -> setSwiftCode($param['swiftCode']);
			$inv -> setIsPo($param['isPo']);
			$inv -> setIsServiceReport($param['isServiceReport']);
			$inv -> setIsDeliverNote($param['isDeliverNote']);
			$inv -> setCompanySignature($param['companySignature']);
			$inv -> setNameSignature($param['nameSignature']);
			$inv -> setPosition($param['position']);
			$inv -> setCurrency($param['currency']);
			$inv -> setTotal($param['total']);
			$inv -> setDpPersen($param['dpPersen']);
			$inv -> setDpValue($param['dpValue']);
			$inv -> setSubTotal($param['subTotal']);
			$inv -> setVarField($param['varField']);
			$inv -> setVarFieldPersen($param['varFieldPersen']);
			$inv -> setVarFieldValue($param['varFieldValue']);
			$inv -> setDisc($param['disc']);
			
			if (array_key_exists('attach',$_FILES)){
				if ($_FILES['attach'] && $_FILES['attach']['error'] == 0) {
					$config['upload_path'] = './attach/';
					$config['allowed_types'] = 'pdf';
					//$config['max_size']	= '100';
					//$config['max_width']  = '1024';
					//$config['max_height']  = '768';
	
					$this -> ci -> load -> library('upload', $config);
	
					if (!$this -> ci -> upload -> do_upload('attach')) {
						$error = array('error' => $this -> ci -> upload -> display_errors());
	
						//$this->load->view('upload_form', $error);
						print_r($error);
						return FALSE;
					} else {
						$data = array('upload_data' => $this -> ci -> upload -> data());
	
						//$this->load->view('upload_success', $data);
						//print_r($data);
						//$inv -> setAttach($data['upload_data']['full_path']);
						$inv -> setAttach('attach/'.$data['upload_data']['file_name']);
					}
				}
			}	

			if (array_key_exists('attach2',$_FILES)){
				if ($_FILES['attach2'] && $_FILES['attach2']['error'] == 0) {
					$config['upload_path'] = './attach/';
					$config['allowed_types'] = 'pdf';
					//$config['max_size']	= '100';
					//$config['max_width']  = '1024';
					//$config['max_height']  = '768';
	
					$this -> ci -> load -> library('upload', $config);
	
					if (!$this -> ci -> upload -> do_upload('attach2')) {
						$error = array('error' => $this -> ci -> upload -> display_errors());
	
						//$this->load->view('upload_form', $error);
						print_r($error);
						return FALSE;
					} else {
						$data = array('upload_data' => $this -> ci -> upload -> data());
	
						//$this->load->view('upload_success', $data);
						//print_r($data);
						//$inv -> setAttach2($data['upload_data']['full_path']);
						$inv -> setAttach2('attach/'.$data['upload_data']['file_name']);
					}
				}
			}
			
			if (array_key_exists('attach3',$_FILES)){
				if ($_FILES['attach3'] && $_FILES['attach3']['error'] == 0) {
					$config['upload_path'] = './attach/';
					$config['allowed_types'] = 'pdf';
					//$config['max_size']	= '100';
					//$config['max_width']  = '1024';
					//$config['max_height']  = '768';
	
					$this -> ci -> load -> library('upload', $config);
	
					if (!$this -> ci -> upload -> do_upload('attach3')) {
						$error = array('error' => $this -> ci -> upload -> display_errors());
	
						//$this->load->view('upload_form', $error);
						print_r($error);
						return FALSE;
					} else {
						$data = array('upload_data' => $this -> ci -> upload -> data());
	
						//$this->load->view('upload_success', $data);
						//print_r($data);
						//$inv -> setAttach3($data['upload_data']['full_path']);
						$inv -> setAttach3('attach/'.$data['upload_data']['file_name']);
					}
				}
			}

			$this -> ci -> em -> persist($inv);
			$this -> ci -> em -> flush();
			$this -> invTaxLastId = $inv -> getId();

			if ($createTax == TRUE) {
				$tax = new \models\entities\Tax;
				$tax -> setInvoiceTax($inv);
				/*$tax -> setKurs2($inv->getCurrency());
				 if ($inv->getCurrency() != 0){
				 $criteria = array('kode' => $inv->getCurrency());
				 $kurs = $this->ci->em->getRepository('\models\entities\Kurs')->findOneBy($criteria);
				 $tax -> setKurs($kurs->getNilai());
				 } else {
				 $tax -> setKurs($inv->getCurrency());
				 }*/

				$this -> ci -> em -> persist($tax);
				$this -> ci -> em -> flush();
			}

			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function findAllInvoiceItemByInvoice($id) {
		$criteria = array('id' => $id);
		$inv = $this -> ci -> em -> getRepository('\models\entities\Invoice') -> findOneBy($criteria);
		$criteria = array('invoice' => $inv);
		$order = array('id' => 'ASC');
		return $this -> ci -> em -> getRepository('\models\entities\InvoiceItem') -> findBy($criteria, $order);
	}

	public function findAllInvoiceTaxItemByInvoice($id) {
		$criteria = array('id' => $id);
		$inv = $this -> ci -> em -> getRepository('\models\entities\InvoiceTax') -> findOneBy($criteria);
		$criteria = array('invoiceTax' => $inv);
		$order = array('id' => 'ASC');
		return $this -> ci -> em -> getRepository('\models\entities\InvoiceTaxItem') -> findBy($criteria, $order);
	}

	public function findAllDelivItemsByDelivNote($id) {
		$criteria = array('id' => $id);
		$delivNote = $this -> ci -> em -> getRepository('\models\entities\DeliveryNote') -> findOneBy($criteria);
		$criteria = array('deliverNote' => $delivNote);
		$order = array('id' => 'ASC');
		return $this -> ci -> em -> getRepository('\models\entities\DeliveryNoteItem') -> findBy($criteria, $order);
	}

	public function saveInvoiceItem($param) {
		if ($param['id'] > 0) {
			$criteria = array('id' => $param['invoice_id']);
			$inv = $this -> ci -> em -> getRepository('\models\entities\Invoice') -> findOneBy($criteria);

			$criteria = array('invoice' => $inv);
			$invItem = $this -> ci -> em -> getRepository('\models\entities\InvoiceItem') -> findBy($criteria);
			//$invItems = $this->ci->em->getRepository('\models\entities\InvoiceItem')->findBy($criteria);
		} else {
			$invItem = new \models\entities\InvoiceItem;
		}

		$invItem -> setDescription($param['description']);
		$invItem -> setQty($param['qty']);
		$invItem -> setPrice($param['price']);
		$invItem -> setUnit($param['unit']);
		$criteria = array('id' => $param['invoice_id']);
		$inv = $this -> ci -> em -> getRepository('\models\entities\Invoice') -> findOneBy($criteria);
		$inv -> setDisc($param['disc']);
		$inv -> setDpPersen($param['dpPersen']);
		$inv -> setDpValue($param['dpValue']);
		$inv -> setTotal($param['total']);
		//$inv -> setSubTotal($param['subTotal']);
		//$inv -> setVarField($param['varField']);
		//$inv -> setVarFieldPersen($param['varFieldPersen']);
		//$inv -> setVarFieldValue($param['varFieldValue']);

		$invItem -> setInvoice($inv);
		$this -> ci -> em -> persist($invItem);
		$this -> ci -> em -> flush();
		$this -> invItemLastId = $invItem -> getId();
	}

	public function saveInvoiceTaxItem($param) {

		if ($param['id'] > 0) {
			$criteria = array('id' => $param['invoice_id']);
			$inv = $this -> ci -> em -> getRepository('\models\entities\InvoiceTax') -> findOneBy($criteria);

			$criteria = array('invoiceTax' => $inv);
			$invItem = $this -> ci -> em -> getRepository('\models\entities\InvoiceTaxItem') -> findBy($criteria);
		} else {
			$invItem = new \models\entities\InvoiceTaxItem;

		}

		$invItem -> setDescription($param['description']);
		$invItem -> setQty($param['qty']);
		$invItem -> setPrice($param['price']);
		$invItem -> setUnit($param['unit']);
		$criteria = array('id' => $param['invoice_id']);
		$inv = $this -> ci -> em -> getRepository('\models\entities\InvoiceTax') -> findOneBy($criteria);
		$inv -> setDpPersen($param['dpPersen']);
		$inv -> setDpValue($param['dpValue']);
		$inv -> setSubTotal($param['subTotal']);
		$inv -> setTotal($param['total']);
		$inv -> setVarField($param['varField']);
		$inv -> setVarFieldPersen($param['varFieldPersen']);
		$inv -> setVarFieldValue($param['varFieldValue']);
		$inv -> setDisc($param['disc']);
		$inv -> setPpn($param['ppn']);
		$invItem -> setInvoiceTax($inv);
		$this -> ci -> em -> persist($invItem);
		$this -> ci -> em -> flush();
		$this -> invTaxItemLastId = $invItem -> getId();

	}

	public function deleteInvoice($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$inv = $this -> ci -> em -> getRepository('\models\entities\Invoice') -> findOneBy($criteria);

			$criteria = array('invoice' => $inv);
			$invItems = $this -> ci -> em -> getRepository('\models\entities\InvoiceItem') -> findBy($criteria);

			if (count($invItems) > 0) {
				foreach ($invItems as $invItem) {
					$this -> deleteInvoiceItem($invItem -> getId());
				}
			}

			$criteria = array('invoice' => $inv);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterInvoice') -> findOneBy($criteria);
			// die(var_dump($counter));
			if ($counter != NULL) {
				$this -> ci -> em -> remove($counter);
				$this -> ci -> em -> flush();
			}

			$this -> ci -> em -> remove($inv);
			$this -> ci -> em -> flush();

			return TRUE;
		}
		return FALSE;
	}

	public function deleteInvoiceTax($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$inv = $this -> ci -> em -> getRepository('\models\entities\InvoiceTax') -> findOneBy($criteria);

			$criteria = array('invoiceTax' => $inv);
			$invItems = $this -> ci -> em -> getRepository('\models\entities\InvoiceTaxItem') -> findBy($criteria);

			if (count($invItems) > 0) {
				foreach ($invItems as $invItem) {
					$this -> deleteInvoiceTaxItem($invItem -> getId());
				}
			}

			$criteria = array('invoiceTax' => $inv);
			$taxs = $this -> ci -> em -> getRepository('\models\entities\Tax') -> findBy($criteria);
			if (count($taxs) > 0) {
				foreach ($taxs as $tax) {
					$this -> deleteTax($tax -> getId());
				}
			}

			$criteria = array('invoiceTax' => $inv);
			$counters = $this -> ci -> em -> getRepository('\models\entities\CounterInvoiceTax') -> findBy($criteria);
			if (count($counters) > 0) {
				foreach ($counters as $counter) {
					$this -> ci -> em -> remove($counter);
					$this -> ci -> em -> flush();
				}
			}

			$this -> ci -> em -> remove($inv);
			$this -> ci -> em -> flush();

			return TRUE;
		}
		return FALSE;
	}

	public function insertInvoiceItem($param) {
		$invItem = new \models\entities\InvoiceItem;
		$invItem -> setDescription($param['description']);
		$invItem -> setQty($param['qty']);
		$invItem -> setUnit($param['unit']);
		$invItem -> setPrice($param['price']);

		$criteria = array('id' => $param['invoice_id']);
		$inv = $this -> ci -> em -> getRepository('\models\entities\Invoice') -> findOneBy($criteria);

		$invItem -> setInvoice($inv);
		$this -> ci -> em -> persist($invItem);
		$this -> ci -> em -> flush();
	}

	public function insertInvoiceTaxItem($param) {
		$invItem = new \models\entities\InvoiceTaxItem;
		$invItem -> setDescription($param['description']);
		$invItem -> setQty($param['qty']);
		$invItem -> setUnit($param['unit']);
		$invItem -> setPrice($param['price']);

		$criteria = array('id' => $param['invoice_id']);
		$inv = $this -> ci -> em -> getRepository('\models\entities\InvoiceTax') -> findOneBy($criteria);

		$invItem -> setInvoiceTax($inv);
		$this -> ci -> em -> persist($invItem);
		$this -> ci -> em -> flush();
	}

	public function deleteInvoiceItem($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$invItem = $this -> ci -> em -> getRepository('\models\entities\InvoiceItem') -> findOneBy($criteria);
			$this -> ci -> em -> remove($invItem);
			$this -> ci -> em -> flush();

			return TRUE;
		}

		return FALSE;
	}

	public function deleteInvoiceTaxItem($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$invItem = $this -> ci -> em -> getRepository('\models\entities\InvoiceTaxItem') -> findOneBy($criteria);
			$this -> ci -> em -> remove($invItem);
			$this -> ci -> em -> flush();

			return TRUE;
		}

		return FALSE;
	}

	// service tax start
	public function insertTax($param) {
		if ($param != null) {
			$tax = new \models\entities\Tax;
			$tax -> setKode($param['kode']);
			$tax -> setNamaPengusaha($param['namaPengusaha']);
			$tax -> setAlamatPengusaha($param['alamatPengusaha']);
			$tax -> setNpwpPengusaha($param['npwpPengusaha']);
			$tax -> setNamaPembeli($param['namaPembeli']);
			$tax -> setAlamatPembeli($param['alamatPembeli']);
			$tax -> setNpwpPembeli($param['npwpPembeli']);
			$tax -> setNote($param['note']);
			
			$taxDate = new \DateTime($param['taxDate'], new \DateTimeZone("Asia/Jakarta"));
			$tax -> setTanggal($taxDate);
			$tax -> setCreatedDate(new \DateTime("now"));
			$tax -> setPajakBarangMewahTarif($param['tarif']);
			$tax -> setPajakBarangMewahDpp($param['dpp']);
			$tax -> setPajakBarangMewahPpnbm($param['ppnbm']);
			$tax -> setPajakBarangMewahTarif2($param['tarif2']);
			$tax -> setPajakBarangMewahDpp2($param['dpp2']);
			$tax -> setPajakBarangMewahPpnbm2($param['ppnbm2']);
			$tax -> setPajakBarangMewahTarif3($param['tarif3']);
			$tax -> setPajakBarangMewahDpp3($param['dpp3']);
			$tax -> setPajakBarangMewahPpnbm3($param['ppnbm3']);
			$tax -> setPajakBarangMewahTarif4($param['tarif4']);
			$tax -> setPajakBarangMewahDpp4($param['dpp4']);
			$tax -> setPajakBarangMewahPpnbm4($param['ppnbm4']);

			$tax -> setJumlah($param['jumlah']);
			$tax -> setKurs($param['kurs']);
			$tax -> setKurs2($param['kurs2']);
			$tax -> setNoteKurs($param['noteKurs']);
			
			$tax -> setTempat($param['tempatTTD']);
			if ($param['tanggalTTD'] != "") {
				$tanggalTTD = new \DateTime($param['tanggalTTD'], new \DateTimeZone("Asia/Jakarta"));
				$tax -> setTanggalTTD($tanggalTTD);
			}
			$tax -> setNama($param['namaTTD']);
			$tax -> setPosisi($param['jabatanTTD']);

			$this -> ci -> em -> persist($tax);
			$this -> ci -> em -> flush();
			$this -> taxLastId = $tax -> getId();
			return TRUE;
		} else {
			return FALSE;
		}

	}

	public function updateTax($param) {
		if ($param != null) {
			$criteria = array('id' => $param['id']);
			$tax = $this -> ci -> em -> getRepository('\models\entities\Tax') -> findOneBy($criteria);
			$tax = new \models\entities\Tax;
			$tax -> setKode($param['kode']);
			$tax -> setNamaPengusaha($param['namaPengusaha']);
			$tax -> setAlamatPengusaha($param['alamatPengusaha']);
			$tax -> setNpwpPengusaha($param['npwpPengusaha']);
			$tax -> setNamaPembeli($param['namaPembeli']);
			$tax -> setAlamatPembeli($param['alamatPembeli']);
			$tax -> setNpwpPembeli($param['npwpPembeli']);
			$tax -> setNote($param['note']);
			
			$taxDate = new \DateTime($param['taxDate'], new \DateTimeZone("Asia/Jakarta"));
			$tax -> setTanggal($taxDate);
			$tax -> setCreatedDate(new \DateTime("now"));
			$tax -> setPajakBarangMewahTarif($param['tarif']);
			$tax -> setPajakBarangMewahDpp($param['dpp']);
			$tax -> setPajakBarangMewahPpnbm($param['ppnbm']);
			$tax -> setPajakBarangMewahTarif2($param['tarif2']);
			$tax -> setPajakBarangMewahDpp2($param['dpp2']);
			$tax -> setPajakBarangMewahPpnbm2($param['ppnbm2']);
			$tax -> setPajakBarangMewahTarif3($param['tarif3']);
			$tax -> setPajakBarangMewahDpp3($param['dpp3']);
			$tax -> setPajakBarangMewahPpnbm3($param['ppnbm3']);
			$tax -> setPajakBarangMewahTarif4($param['tarif4']);
			$tax -> setPajakBarangMewahDpp4($param['dpp4']);
			$tax -> setPajakBarangMewahPpnbm4($param['ppnbm4']);

			$tax -> setJumlah($param['jumlah']);
			$tax -> setKurs($param['kurs']);
			$tax -> setKurs2($param['kurs2']);
			$tax -> setNoteKurs($param['noteKurs']);
			
			$tax -> setTempat($param['tempatTTD']);
			if ($param['tanggalTTD'] != "") {
				$tanggalTTD = new \DateTime($param['tanggalTTD'], new \DateTimeZone("Asia/Jakarta"));
				$tax -> setTanggalTTD($tanggalTTD);
			}
			$tax -> setNama($param['namaTTD']);
			$tax -> setPosisi($param['jabatanTTD']);

			$this -> ci -> em -> persist($tax);
			$this -> ci -> em -> flush();
			$this -> taxLastId = $inv -> getId();
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function saveTaxItem($param) {
		if ($param['id'] == 0) {
			$taxItem = new \models\entities\TaxItem;
			$taxItem -> setDeskripsi($param['description']);

			$criteria = array('id' => $param['tax_id']);
			$tax = $this -> ci -> em -> getRepository('\models\entities\Tax') -> findOneBy($criteria);

			$taxItem -> setHargaJual($param['price']);
			$taxItem -> setTax($tax);

			$this -> ci -> em -> persist($taxItem);
			$this -> ci -> em -> flush();
			$this -> taxItemLastId = $taxItem -> getId();
			return TRUE;
		} else if ($param['id'] > 0) {
			$criteria = array('id' => $param['id']);
			$taxItem = $this -> ci -> em -> getRepository('\models\entities\TaxItem') -> findOneBy($criteria);

			$taxItem -> setDeskripsi($param['description']);
			$taxItem -> setHargaJual($param['price']);

			$this -> ci -> em -> persist($taxItem);
			$this -> ci -> em -> flush();
			$this -> taxItemLastId = $taxItem -> getId();
			return TRUE;
		}
	}

	public function deleteTax($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$tax = $this -> ci -> em -> getRepository('\models\entities\Tax') -> findOneBy($criteria);

			$criteria = array('tax' => $tax);
			$taxItems = $this -> ci -> em -> getRepository('\models\entities\TaxItem') -> findBy($criteria);

			if (count($taxItems) > 0) {
				foreach ($taxItems as $taxItem) {
					$this -> deleteTaxItem($taxItem -> getId());
				}
			}

			$this -> ci -> em -> remove($tax);
			$this -> ci -> em -> flush();

			return TRUE;
		}
		return FALSE;
	}

	public function deleteTaxItem($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$taxItem = $this -> ci -> em -> getRepository('\models\entities\TaxItem') -> findOneBy($criteria);
			$this -> ci -> em -> remove($taxItem);
			$this -> ci -> em -> flush();

			return TRUE;
		}

		return FALSE;
	}

	// end service tax

	// start delivery note
	public function findAllDelivItemByDeliv($id) {
		$criteria = array('id' => $id);
		$deliv = $this -> ci -> em -> getRepository('\models\entities\DeliveryNote') -> findOneBy($criteria);
		$criteria = array('deliverNote' => $deliv);
		$order = array('id' => 'ASC');
		return $this -> ci -> em -> getRepository('\models\entities\DeliveryNoteItem') -> findBy($criteria, $order);
	}

	public function saveDeliveryNote($param) {
		// echo "<pre>";print_r($param);die();
		if ($param != null) {
			$mode = "save";
			if ($param['id'] > 0) {
				$criteria = array('id' => $param['id']);
				$delivNote = $this -> ci -> em -> getRepository('\models\entities\DeliveryNote') -> findOneBy($criteria);
			} else {
				$delivNote = new \models\entities\DeliveryNote;
				$mode = "add";
			}

			$delivNote -> setCustName($param['custName']);
			$delivNote -> setCustAddress($param['custAddress']);
			$delivNote -> setNo($param['no']);
			if ($param['company'] > 0){
				$criteria = array('id' => $param['company']);
				$company = $this->ci->em->getRepository('\models\entities\Company')->findOneBy($criteria);
				$delivNote -> setCompany($company);	
			}
			if ($param['deliveryDate'] != "") {
				$deliveryDate = new \DateTime($param['deliveryDate'], new \DateTimeZone("Asia/Jakarta"));
				$delivNote -> setDeliveryDate($deliveryDate);
			} else {
				$delivNote -> setDeliveryDate(NULL);
			}

			// if ($mode == "add"){
			// $inv -> setCreatedDate(new \DateTime("now"));
			// }

			$delivNote -> setOrderReff($param['orderReff']);

			$delivNote -> setNote($param['note']);

			$delivNote -> setNoteDeliv($param['noteDeliv']);

			$delivNote -> setCompSignature($param['compSign']);
			$delivNote -> setNameSignature($param['nameSign']);
			$delivNote -> setSignaturePosition($param['positionSign']);

			$delivNote -> setCustAuthname($param['custAuthname']);
			$delivNote -> setCustAuthposition($param['custAuthposition']);

			$this -> ci -> em -> persist($delivNote);
			$this -> ci -> em -> flush();
			$this -> delivNoteLastId = $delivNote -> getId();
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function saveDelivItem($param) {

		$criteria = array('id' => $param['deliveryNote_id']);
		$delivNote = $this -> ci -> em -> getRepository('\models\entities\DeliveryNote') -> findOneBy($criteria);
		if ($param['id'] > 0) {

			$criteria = array('deliverNote' => $delivNote);
			$deliverItems = $this -> ci -> em -> getRepository('\models\entities\DeliveryNoteItem') -> findBy($criteria);

		} else {
			$delivItem = new \models\entities\DeliveryNoteItem;
		}

		$delivItem -> setDescription($param['description']);
		$delivItem -> setQty($param['qty']);
		$delivItem -> setUnit($param['unit']);
		$delivItem -> setDeliverNote($delivNote);
		$this -> ci -> em -> persist($delivItem);
		$this -> ci -> em -> flush();
		$this -> delivItemLastId = $delivItem -> getId();
	}

	public function deleteDelivItem($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$delivItem = $this -> ci -> em -> getRepository('\models\entities\DeliveryNoteItem') -> findOneBy($criteria);
			$this -> ci -> em -> remove($delivItem);
			$this -> ci -> em -> flush();

			return TRUE;
		}

		return FALSE;
	}

	public function duplicateDeliveryNote($deliv) {
		if ($deliv -> getId() > 0) {
			$newDeliv = new \models\entities\DeliveryNote;

			// get counter
			$criteria = array('deliv' => $deliv);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterDeliveryNote') -> findOneBy($criteria);

			if ($counter != NULL) {
				$no = $deliv -> getNo() . "(" . ($counter -> getCounter() + 1) . ")";
				$counter -> setCounter($counter -> getCounter() + 1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			} else {
				$no = $deliv -> getNo() . "(1)";
				$counter = new \models\entities\CounterDeliveryNote;
				$counter -> setDeliv($deliv);
				$counter -> setCounter(1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			}
			
			$newDeliv -> setCompany($deliv -> getCompany());
				
			$newDeliv -> setCustName($deliv -> getCustName());
			$newDeliv -> setCustAddress($deliv -> getCustAddress());
			$newDeliv -> setNo($no);
			// $newDeliv -> setDeliveryDate($deliv -> getDeliveryDate());
			$newDeliv -> setDeliveryDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
			$newDeliv -> setOrderReff($deliv -> getOrderReff());
			$newDeliv -> setNote($deliv -> getNote());
			$newDeliv -> setNoteDeliv($deliv -> getNoteDeliv());
			$newDeliv -> setCompSignature($deliv -> getCompSignature());
			$newDeliv -> setNameSignature($deliv -> getNameSignature());
			$newDeliv -> setSignaturePosition($deliv -> getSignaturePosition());
			$newDeliv -> setCustAuthname($deliv -> getCustAuthname());
			$newDeliv -> setCustAuthposition($deliv -> getCustAuthposition());
			$this -> ci -> em -> persist($newDeliv);
			$this -> ci -> em -> flush();

			return $newDeliv;
		}
		return FALSE;
	}

	public function deleteDeliveryNote($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$deliveryNote = $this -> ci -> em -> getRepository('\models\entities\DeliveryNote') -> findOneBy($criteria);

			$criteria = array('deliverNote' => $deliveryNote);
			$delivItems = $this -> ci -> em -> getRepository('\models\entities\DeliveryNoteItem') -> findBy($criteria);

			if (count($delivItems) > 0) {
				foreach ($delivItems as $delivItem) {
					$this -> deleteDelivItem($delivItem -> getId());
				}
			}

			$criteria = array('deliv' => $deliveryNote);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterDeliveryNote') -> findOneBy($criteria);

			if ($counter != NULL) {
				$this -> ci -> em -> remove($counter);
				$this -> ci -> em -> flush();
			}

			$this -> ci -> em -> remove($deliveryNote);
			$this -> ci -> em -> flush();

			return TRUE;
		}
		return FALSE;
	}

	public function insertDelivItem($param) {
		$delivItem = new \models\entities\DeliveryNoteItem;
		$delivItem -> setDescription($param['description']);
		$delivItem -> setQty($param['qty']);
		$delivItem -> setUnit($param['unit']);

		$criteria = array('id' => $param['deliv_id']);
		$deliv = $this -> ci -> em -> getRepository('\models\entities\DeliveryNote') -> findOneBy($criteria);

		$delivItem -> setDeliverNote($deliv);
		$this -> ci -> em -> persist($delivItem);
		$this -> ci -> em -> flush();
	}

	// END DELIVERY ITEM

	// START QUOTATION
	public function saveQuotation($param) {
		// echo "<pre>";print_r($param);die();
		if ($param != null) {
			$mode = "save";
			if ($param['id'] > 0) {
				$criteria = array('id' => $param['id']);
				$quotation = $this -> ci -> em -> getRepository('\models\entities\Quotation') -> findOneBy($criteria);
			} else {
				$quotation = new \models\entities\Quotation;
				$mode = "add";
			}
			if ($param['company'] > 0){
				$criteria = array('id' => $param['company']);
				$company = $this->ci->em->getRepository('\models\entities\Company')->findOneBy($criteria);
				$quotation -> setCompany($company);	
			}
			$quotation -> setCompanyName($param['companyName']);
			$quotation -> setCompanyAddress($param['companyAddress']);
			$quotation -> setQuotationNo($param['quotationNo']);
			$quotation -> setRevisionNo($param['revisionNo']);
			if ($param['quotationDate'] != "") {
				$quotationDate = new \DateTime($param['quotationDate'], new \DateTimeZone("Asia/Jakarta"));
				$quotation -> setQuotationDate($quotationDate);
			} else {
				$quotation -> setQuotationDate(NULL);
			}

			$quotation -> setAttn($param['attn']);
			$quotation -> setSubject($param['subject']);
			$quotation -> setIntroduction($param['introduction']);
			$quotation -> setNote($param['note']);
			$quotation -> setNoteTc($param['noteTc']);
			$quotation -> setCurrency($param['currency']);
			$quotation -> setValidity($param['validity']);
			$quotation -> setDelivery($param['delivery']);
			$quotation -> setTermPayment($param['termPayment']);
			$quotation -> setWorkPeriod($param['workPeriod']);
			$quotation -> setEndClosure($param['endClosure']);
			$quotation -> setCompanySignature($param['companySignature']);
			$quotation -> setNameSignature($param['nameSignature']);
			$quotation -> setPositionSignature($param['positionSignature']);

			if ($_FILES['attach'] && $_FILES['attach']['error'] == 0) {
				$config['upload_path'] = './attach/';
				$config['allowed_types'] = 'jpg';
				//$config['max_size']	= '100';
				//$config['max_width']  = '1024';
				//$config['max_height']  = '768';

				$this -> ci -> load -> library('upload', $config);

				if (!$this -> ci -> upload -> do_upload('attach')) {
					$error = array('error' => $this -> ci -> upload -> display_errors());

					//$this->load->view('upload_form', $error);
					print_r($error);
					return FALSE;
				} else {
					$data = array('upload_data' => $this -> ci -> upload -> data());

					//$this->load->view('upload_success', $data);
					//print_r($data);
					$quotation -> setTtdImage('attach/'.$data['upload_data']['file_name']);
				}
			}
			
			$this -> ci -> em -> persist($quotation);
			$this -> ci -> em -> flush();
			$this -> quotationLastId = $quotation -> getId();
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function saveQuotationItem($param) {

		if ($param['id'] > 0) {
			$criteria = array('id' => $param['quotation_id']);
			$quotation = $this -> ci -> em -> getRepository('\models\entities\Quotation') -> findOneBy($criteria);

			$criteria = array('quotation' => $quotation);
			$quotationItem = $this -> ci -> em -> getRepository('\models\entities\QuotationItem') -> findBy($criteria);
		} else {
			$quotationItem = new \models\entities\QuotationItem;

			$criteria = array('id' => $param['quotation_id']);
			$quotation = $this -> ci -> em -> getRepository('\models\entities\Quotation') -> findOneBy($criteria);
		}

		$quotationItem -> setDescription($param['description']);
		$quotationItem -> setQty($param['qty']);
		$quotationItem -> setPrice($param['price']);
		$quotationItem -> setUnit($param['unit']);
		$quotationItem -> setQuotation($quotation);
		$this -> ci -> em -> persist($quotationItem);
		$this -> ci -> em -> flush();
		$this -> quotationItemLastId = $quotationItem -> getId();
	}

	public function deleteQuotationItem($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$quotationItem = $this -> ci -> em -> getRepository('\models\entities\QuotationItem') -> findOneBy($criteria);
			$this -> ci -> em -> remove($quotationItem);
			$this -> ci -> em -> flush();

			return TRUE;
		}

		return FALSE;
	}

	public function deleteQuotation($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$quotation = $this -> ci -> em -> getRepository('\models\entities\Quotation') -> findOneBy($criteria);

			$criteria = array('quotation' => $quotation);
			$quotationItems = $this -> ci -> em -> getRepository('\models\entities\QuotationItem') -> findBy($criteria);

			if (count($quotationItems) > 0) {
				foreach ($quotationItems as $quotationItem) {
					$this -> deleteQuotationItem($quotationItem -> getId());
				}
			}

			$criteria = array('quotation' => $quotation);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterQuotation') -> findOneBy($criteria);

			if ($counter != NULL) {
				$this -> ci -> em -> remove($counter);
				$this -> ci -> em -> flush();
			}

			$this -> ci -> em -> remove($quotation);
			$this -> ci -> em -> flush();

			return TRUE;
		}
		return FALSE;
	}

	public function duplicateQuotation($quotation) {
		if ($quotation -> getId() > 0) {
			$newQuo = new \models\entities\Quotation;
			$newQuo -> setCompanyName($quotation -> getCompanyName());
			$newQuo -> setCompanyAddress($quotation -> getCompanyAddress());
			// $quotation->setQuotationNo($quotation->getQuotationNo());

			// get counter
			$criteria = array('quotation' => $quotation);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterQuotation') -> findOneBy($criteria);
			//var_dump($counter);
			if ($counter != NULL) {
				$quotationNo = $quotation -> getQuotationNo() . "(" . ($counter -> getCounter() + 1) . ")";
				$counter -> setCounter($counter -> getCounter() + 1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			} else {
				$quotationNo = $quotation -> getQuotationNo() . "(1)";
				$counter = new \models\entities\CounterQuotation;
				$counter -> setQuotation($quotation);
				$counter -> setCounter(1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			}

			$newQuo -> setCompany($quotation -> getCompany());
			
			$newQuo -> setQuotationNo($quotationNo);
			$newQuo -> setRevisionNo($quotation -> getRevisionNo());
			$newQuo -> setAttn($quotation -> getAttn());
			$newQuo -> setSubject($quotation -> getSubject());
			$newQuo -> setIntroduction($quotation -> getIntroduction());
			$newQuo -> setNote($quotation -> getNote());
			$newQuo -> setNoteTc($quotation -> getNoteTc());
			$newQuo -> setValidity($quotation -> getValidity());
			$newQuo -> setDelivery($quotation -> getDelivery());
			$newQuo -> setTermPayment($quotation -> getTermPayment());
			$newQuo -> setWorkPeriod($quotation -> getWorkPeriod());
			$newQuo -> setEndClosure($quotation -> getEndClosure());
			$newQuo -> setCompanySignature($quotation -> getCompanySignature());
			$newQuo -> setNameSignature($quotation -> getNameSignature());
			$newQuo -> setPositionSignature($quotation -> getPositionSignature());
			$newQuo -> setTtdImage($quotation->getTtdImage());
			$newQuo -> setCurrency($quotation->getCurrency());
			if ($quotation -> getQuotationDate() != "") {
				// $newQuo -> setQuotationDate($quotation -> getQuotationDate());
				$newQuo -> setQuotationDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
				
			}

			$this -> ci -> em -> persist($newQuo);
			$this -> ci -> em -> flush();

			return $newQuo;
		}
		return FALSE;
	}

	public function findAllQuotation($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\Quotation') -> findAllAsObject();
	}

	public function findAllQuotationItemByQuotation($id) {
		$criteria = array('id' => $id);
		$quotation = $this -> ci -> em -> getRepository('\models\entities\Quotation') -> findOneBy($criteria);
		$criteria = array('quotation' => $quotation);
		$order = array('id' => 'ASC');
		return $this -> ci -> em -> getRepository('\models\entities\QuotationItem') -> findBy($criteria, $order);
	}

	public function insertQuotationItem($param) {
		$quotationItem = new \models\entities\QuotationItem;
		$quotationItem -> setDescription($param['description']);
		$quotationItem -> setQty($param['qty']);
		$quotationItem -> setUnit($param['unit']);
		$quotationItem -> setPrice($param['price']);

		$criteria = array('id' => $param['quotation_id']);
		$quotation = $this -> ci -> em -> getRepository('\models\entities\Quotation') -> findOneBy($criteria);

		$quotationItem -> setQuotation($quotation);
		$this -> ci -> em -> persist($quotationItem);
		$this -> ci -> em -> flush();
	}

	// END QUOTATION

	// START INQUIRY
	public function findAllInquiry($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\Inquiry') -> findAllAsObject();
	}

	public function findAllInquiryItemByInquiry($id) {
		$criteria = array('id' => $id);
		$inquiry = $this -> ci -> em -> getRepository('\models\entities\Inquiry') -> findOneBy($criteria);
		$criteria = array('inquiry' => $inquiry);
		$order = array('id' => 'ASC');
		return $this -> ci -> em -> getRepository('\models\entities\InquiryItem') -> findBy($criteria, $order);
	}

	public function saveInquiry($param) {
		// echo "<pre>";print_r($param);die();
		if ($param != null) {
			$mode = "save";
			if ($param['id'] > 0) {
				$criteria = array('id' => $param['id']);
				$inquiry = $this -> ci -> em -> getRepository('\models\entities\Inquiry') -> findOneBy($criteria);
			} else {
				$inquiry = new \models\entities\Inquiry;
				$mode = "add";
			}

			$inquiry -> setCompanyName($param['companyName']);
			$inquiry -> setCompanyAddress($param['companyAddress']);
			$inquiry -> setInquiryNo($param['inquiryNo']);
			if ($param['company'] > 0){
				$criteria = array('id' => $param['company']);
				$company = $this->ci->em->getRepository('\models\entities\Company')->findOneBy($criteria);
				$inquiry-> setCompany($company);	
			}
			if ($param['inquiryDate'] != "") {
				$inquiryDate = new \DateTime($param['inquiryDate'], new \DateTimeZone("Asia/Jakarta"));
				$inquiry -> setInquiryDate($inquiryDate);
			} else {
				$inquiry -> setInquiryDate(NULL);
			}

			$inquiry -> setAttn($param['attn']);
			$inquiry -> setIntroduction($param['introduction']);
			$inquiry -> setNote($param['note']);
			$inquiry -> setEndClosure($param['endClosure']);
			$inquiry -> setCompanySignature($param['companySignature']);
			$inquiry -> setNameSignature($param['nameSignature']);
			$inquiry -> setPositionSignature($param['positionSignature']);
	
			if ($_FILES['attach'] && $_FILES['attach']['error'] == 0) {
				$config['upload_path'] = './attach/';
				$config['allowed_types'] = 'jpg';
				//$config['max_size']	= '100';
				//$config['max_width']  = '1024';
				//$config['max_height']  = '768';

				$this -> ci -> load -> library('upload', $config);

				if (!$this -> ci -> upload -> do_upload('attach')) {
					$error = array('error' => $this -> ci -> upload -> display_errors());

					//$this->load->view('upload_form', $error);
					print_r($error);
					return FALSE;
				} else {
					$data = array('upload_data' => $this -> ci -> upload -> data());

					//$this->load->view('upload_success', $data);
					//print_r($data);
					$inquiry -> setTtdImage('attach/'.$data['upload_data']['file_name']);
				}
			}
			$this -> ci -> em -> persist($inquiry);
			$this -> ci -> em -> flush();
			$this -> inquiryLastId = $inquiry -> getId();
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function saveInquiryItem($param) {

		$criteria = array('id' => $param['inquiry_id']);
		$inquiry = $this -> ci -> em -> getRepository('\models\entities\Inquiry') -> findOneBy($criteria);

		if ($param['id'] > 0) {
			$criteria = array('inquiry' => $inquiry);
			$inquiryItem = $this -> ci -> em -> getRepository('\models\entities\InquiryItem') -> findBy($criteria);
		} else {
			$inquiryItem = new \models\entities\InquiryItem;
		}

		$inquiryItem -> setDescription($param['description']);
		$inquiryItem -> setQty($param['qty']);
		$inquiryItem -> setUnit($param['unit']);
		$inquiryItem -> setInquiry($inquiry);
		$this -> ci -> em -> persist($inquiryItem);
		$this -> ci -> em -> flush();
		$this -> inquiryItemLastId = $inquiryItem -> getId();
	}

	public function deleteInquiry($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$inquiry = $this -> ci -> em -> getRepository('\models\entities\Inquiry') -> findOneBy($criteria);

			$criteria = array('inquiry' => $inquiry);
			$inquiryItems = $this -> ci -> em -> getRepository('\models\entities\InquiryItem') -> findBy($criteria);

			if (count($inquiryItems) > 0) {
				foreach ($inquiryItems as $inquiryItem) {
					$this -> deleteInquiryItem($inquiryItem -> getId());
				}
			}

			$criteria = array('inquiry' => $inquiry);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterInquiry') -> findOneBy($criteria);

			if ($counter != NULL) {
				$this -> ci -> em -> remove($counter);
				$this -> ci -> em -> flush();
			}

			$this -> ci -> em -> remove($inquiry);
			$this -> ci -> em -> flush();

			return TRUE;
		}
		return FALSE;
	}

	public function deleteInquiryItem($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$inquiryItem = $this -> ci -> em -> getRepository('\models\entities\InquiryItem') -> findOneBy($criteria);
			$this -> ci -> em -> remove($inquiryItem);
			$this -> ci -> em -> flush();

			return TRUE;
		}

		return FALSE;
	}

	public function duplicateInquiry($inquiry) {
		if ($inquiry -> getId() > 0) {
			$newInq = new \models\entities\Inquiry;
			$newInq -> setCompanyName($inquiry -> getCompanyName());
			$newInq -> setCompanyAddress($inquiry -> getCompanyAddress());
			$newInq -> setAttn($inquiry -> getAttn());

			// get counter
			$criteria = array('inquiry' => $inquiry);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterInquiry') -> findOneBy($criteria);
			//var_dump($counter);
			if ($counter != NULL) {
				$inquiryNo = $inquiry -> getInquiryNo() . "(" . ($counter -> getCounter() + 1) . ")";
				$counter -> setCounter($counter -> getCounter() + 1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			} else {
				$inquiryNo = $inquiry -> getInquiryNo() . "(1)";
				$counter = new \models\entities\CounterInquiry;
				$counter -> setInquiry($inquiry);
				$counter -> setCounter(1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			}

			$newInq -> setCompany($inquiry -> getCompany());

			$newInq -> setInquiryNo($inquiryNo);
			$newInq -> setIntroduction($inquiry -> getIntroduction());
			$newInq -> setNote($inquiry -> getNote());
			$newInq -> setEndClosure($inquiry -> getEndClosure());
			$newInq -> setCompanySignature($inquiry -> getCompanySignature());
			$newInq -> setNameSignature($inquiry -> getNameSignature());
			$newInq -> setPositionSignature($inquiry -> getPositionSignature());
			$newInq -> setTtdImage($inquiry->getTtdImage());
			if ($inquiry -> getInquiryDate() != "") {
				// $newInq -> setInquiryDate($inquiry -> getInquiryDate());
				$newInq -> setInquiryDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
				
			}

			$this -> ci -> em -> persist($newInq);
			$this -> ci -> em -> flush();

			return $newInq;
		}
		return FALSE;
	}

	public function insertInquiryItem($param) {
		$inqItem = new \models\entities\InquiryItem;
		$inqItem -> setDescription($param['description']);
		$inqItem -> setQty($param['qty']);
		$inqItem -> setUnit($param['unit']);

		$criteria = array('id' => $param['inquiry_id']);
		$inq = $this -> ci -> em -> getRepository('\models\entities\Inquiry') -> findOneBy($criteria);

		$inqItem -> setInquiry($inq);
		$this -> ci -> em -> persist($inqItem);
		$this -> ci -> em -> flush();
	}

	// END INQUIRY

	// START PO
	public function findAllPo($indexAndCount) {
		return $this -> ci -> em -> getRepository('\models\entities\Po') -> findAllAsObject();
	}

	public function findAllPoItemByPo($id) {
		$criteria = array('id' => $id);
		$po = $this -> ci -> em -> getRepository('\models\entities\Po') -> findOneBy($criteria);
		$criteria = array('po' => $po);
		return $this -> ci -> em -> getRepository('\models\entities\PoItem') -> findBy($criteria);
	}

	public function savePo($param) {
		// echo "<pre>";print_r($param);die();
		if ($param != null) {
			$mode = "save";
			if ($param['id'] > 0) {
				$criteria = array('id' => $param['id']);
				$po = $this -> ci -> em -> getRepository('\models\entities\Po') -> findOneBy($criteria);
			} else {
				$po = new \models\entities\Po;
				$mode = "add";
			}

			$po -> setNo($param['no']);
			$po -> setSupplierName($param['supplierName']);
			$po -> setSupplierAddress($param['supplierAddress']);
			$po -> setPhone($param['phone']);
			$po -> setFax($param['fax']);
			$po -> setReff($param['reff']);
			$po -> setAttn($param['attn']);
			$po -> setNote($param['note']);
			$po -> setTotalOrder($param['totalOrder']);
			$po -> setDeliveryTime($param['deliveryTime']);
			$po -> setDeliveryTo($param['deliveryTo']);
			$po -> setPayment($param['payment']);
			$po -> setNoteAccept($param['noteAccept']);
			$po -> setSignAccept($param['signAccept']);
			$po -> setDateAccept($param['dateAccept']);
			$po -> setPpn($param['ppn']);
			$po -> setCurrency($param['currency']);
			// var_dump($param['poDate']);die();
			if ($param['poDate'] != "") {
				$poDate = new \DateTime($param['poDate'], new \DateTimeZone("Asia/Jakarta"));
				$po -> setPoDate($poDate);
			} else {
				$po -> setPoDate(NULL);
			}
			
			if ($param['company'] > 0){
				$criteria = array('id' => $param['company']);
				$company = $this->ci->em->getRepository('\models\entities\Company')->findOneBy($criteria);
				$po -> setCompany($company);	
			}
			
			$po -> setCompanySignature($param['companySignature']);
			$po -> setNameSignature($param['nameSignature']);
			$po -> setPositionSignature($param['positionSignature']);

			if ($_FILES['attach'] && $_FILES['attach']['error'] == 0) {
				$config['upload_path'] = './attach/';
				$config['allowed_types'] = 'jpg';
				//$config['max_size']	= '100';
				//$config['max_width']  = '1024';
				//$config['max_height']  = '768';

				$this -> ci -> load -> library('upload', $config);

				if (!$this -> ci -> upload -> do_upload('attach')) {
					$error = array('error' => $this -> ci -> upload -> display_errors());

					//$this->load->view('upload_form', $error);
					print_r($error);
					return FALSE;
				} else {
					$data = array('upload_data' => $this -> ci -> upload -> data());

					//$this->load->view('upload_success', $data);
					//print_r($data);
					$po -> setTtdImage('attach/'.$data['upload_data']['file_name']);
				}
			}
			
			$this -> ci -> em -> persist($po);
			$this -> ci -> em -> flush();
			$this -> poLastId = $po -> getId();
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function savePoItem($param) {

		$criteria = array('id' => $param['po_id']);
		$po = $this -> ci -> em -> getRepository('\models\entities\Po') -> findOneBy($criteria);

		if ($param['id'] > 0) {
			$criteria = array('po' => $po);
			$poItem = $this -> ci -> em -> getRepository('\models\entities\PoItem') -> findBy($criteria);
		} else {
			$poItem = new \models\entities\PoItem;
		}

		$poItem -> setDescription($param['description']);
		$poItem -> setQty($param['qty']);
		$poItem -> setUnit($param['unit']);
		$poItem -> setPrice($param['price']);
		$poItem -> setPo($po);
		$this -> ci -> em -> persist($poItem);
		$this -> ci -> em -> flush();
		$this -> poItemLastId = $poItem -> getId();
	}

	public function deletePoItem($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$poItem = $this -> ci -> em -> getRepository('\models\entities\PoItem') -> findOneBy($criteria);
			$this -> ci -> em -> remove($poItem);
			$this -> ci -> em -> flush();

			return TRUE;
		}

		return FALSE;
	}

	public function duplicatePo($po) {
		if ($po -> getId() > 0) {
			$newPo = new \models\entities\Po;

			// get counter
			$criteria = array('po' => $po);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterPo') -> findOneBy($criteria);

			if ($counter != NULL) {
				$poNo = $po -> getNo() . "(" . ($counter -> getCounter() + 1) . ")";
				$counter -> setCounter($counter -> getCounter() + 1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			} else {
				$poNo = $po -> getNo() . "(1)";
				$counter = new \models\entities\CounterPo;
				$counter -> setPo($po);
				$counter -> setCounter(1);
				$this -> ci -> em -> persist($counter);
				$this -> ci -> em -> flush();
			}

			$newPo -> setCompany($po -> getCompany());

			$newPo -> setNo($poNo);
			$newPo -> setSupplierName($po -> getSupplierName());
			$newPo -> setSupplierAddress($po -> getSupplierAddress());
			$newPo -> setPhone($po -> getPhone());
			$newPo -> setFax($po -> getFax());
			$newPo -> setReff($po -> getReff());
			$newPo -> setAttn($po -> getAttn());
			$newPo -> setNote($po -> getNote());
			$newPo -> setTotalOrder($po -> getTotalOrder());
			$newPo -> setDeliveryTime($po -> getDeliveryTime());
			$newPo -> setDeliveryTo($po -> getDeliveryTo());
			$newPo -> setPayment($po -> getPayment());
			$newPo -> setNoteAccept($po -> getNoteAccept());
			$newPo -> setSignAccept($po -> getSignAccept());
			$newPo -> setPpn($po -> getPpn());
			$newPo -> setCurrency($po -> getCurrency());
			$newPo -> setTtdImage($po -> getTtdImage());
			// var_dump($param['poDate']);die();
			if ($po -> getPoDate() != "") {
				//$poDate = new \DateTime($po->getPoDate(), new \DateTimeZone("Asia/Jakarta"));
				// $newPo -> setPoDate($po -> getPoDate());
				$newPo -> setPoDate(new \DateTime('now', new \DateTimeZone("Asia/Jakarta")));
				
			} else {
				$newPo -> setPoDate(NULL);
			}

			if ($po -> getDateAccept() != "") {
				//$dateAccept = new \DateTime($po->getDateAccept(), new \DateTimeZone("Asia/Jakarta"));
				$newPo -> setDateAccept($po -> getDateAccept());
			} else {
				$newPo -> setDateAccept(NULL);
			}

			$newPo -> setCompanySignature($po -> getCompanySignature());
			$newPo -> setNameSignature($po -> getNameSignature());
			$newPo -> setPositionSignature($po -> getPositionSignature());

			$this -> ci -> em -> persist($newPo);
			$this -> ci -> em -> flush();

			return $newPo;
		}
		return FALSE;
	}

	public function insertPoItem($param) {
		$poItem = new \models\entities\PoItem;
		$poItem -> setDescription($param['description']);
		$poItem -> setQty($param['qty']);
		$poItem -> setUnit($param['unit']);
		$poItem -> setPrice($param['price']);

		$criteria = array('id' => $param['po_id']);
		$po = $this -> ci -> em -> getRepository('\models\entities\Po') -> findOneBy($criteria);

		$poItem -> setPo($po);
		$this -> ci -> em -> persist($poItem);
		$this -> ci -> em -> flush();
	}

	public function deletePo($id) {
		if ($id > 0) {
			$criteria = array('id' => $id);
			$po = $this -> ci -> em -> getRepository('\models\entities\Po') -> findOneBy($criteria);

			$criteria = array('po' => $po);
			$poItems = $this -> ci -> em -> getRepository('\models\entities\PoItem') -> findBy($criteria);

			if (count($poItems) > 0) {
				foreach ($poItems as $poItem) {
					$this -> deletePoItem($poItem -> getId());
				}
			}

			$criteria = array('po' => $po);
			$counter = $this -> ci -> em -> getRepository('\models\entities\CounterPo') -> findOneBy($criteria);

			if ($counter != NULL) {
				$this -> ci -> em -> remove($counter);
				$this -> ci -> em -> flush();
			}

			$this -> ci -> em -> remove($po);
			$this -> ci -> em -> flush();

			return TRUE;
		}
		return FALSE;
	}

	// END PO

	public function duplicateStock($stock) {
		if ($stock -> getId() > 0) {
			$newItem = new \models\entities\Item;
			$newItem -> setName($stock -> getName());
			$newItem -> setDescription($stock -> getDescription());
			$newItem -> setMerk($stock -> getMerk());
			$newItem -> setQty($stock -> getQty());
			$newItem -> setPrice($stock -> getPrice());
			$newItem -> setUnit($stock -> getUnit());
			$newItem -> setCategory($stock -> getCategory());
			$newItem -> setSerialNo($stock -> getSerialNo());
			$newItem -> setModelNo($stock -> getModelNo());
			$newItem -> setCurrency($stock -> getCurrency());
			$newItem -> setNote($stock -> getNote());
			$newItem -> setNoteAdmin($stock -> getNoteAdmin());
			$newItem -> setAttach($stock->getAttach());
			$newItem -> setAttach2($stock->getAttach2());
			$newItem -> setAttach3($stock->getAttach3());
			$this -> ci -> em -> persist($newItem);
			$this -> ci -> em -> flush();

			return $newItem;
		}
		return FALSE;
	}

}
